namespace StrongOA.Web.Controllers.Api
{
    public class FullElectronInvoiceApiController : ApiController
    {

        ///// <summary>
        /// 测试aes
        ///// </summary>
        public void test()
        {
            try
            {
                var b = "k3RA2bS97JlV5XPEueiqUA==";
                var c = AesEncryptor_Hex("中国test", "._^BV67nW6ck8fwg");
                var d = AesDecryptor_Hex(c, "._^BV67nW6ck8fwg");
            }
            catch (Exception ec)
            {
                throw ec;
            }
        }

        /// <summary>
        ///AES 算法加密(ECB模式) 将明文加密，加密后进行Hex编码，返回密文
        /// </summary>
        /// <param name="str">明文</param>
        /// <param name="key">密钥</param>
        /// <returns>加密后Hex编码的密文</returns>
        public static string AesEncryptor_Hex(string input, string key)
        {
            byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 16));
            using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
            {
                aesAlg.Padding = PaddingMode.PKCS7;
                aesAlg.Mode = CipherMode.ECB;
                aesAlg.Key = keyBytes;
                aesAlg.IV = keyBytes;
                ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {
                            swEncrypt.Write(input);
                        }
                        byte[] bytes = msEncrypt.ToArray();
                        //var ddc = Convert.ToBase64String(bytes);
                        //return ddc;
                        //byte[] inputBytes = Convert.FromBase64String(ddc);
                        var hexStr = ByteArrayToHexString(bytes).ToLower();
                        return hexStr;
                    }
                }
            }
        }

        /// <summary>
        ///AES 算法解密(ECB模式) 将密文Hex解码后进行解密，返回明文
        /// </summary> AesDecryptor_Hex
        /// <param name="str">密文</param>
        /// <param name="key">密钥</param>
        /// <returns>明文</returns>
        public static string AesDecryptor_Hex(string input, string key)
        {
            //byte[] inputBytes = Convert.FromBase64String(input);
            byte[] inputBytes = HexStringToByteArray(input);
            byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 16));
            using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
            {
                aesAlg.Padding = PaddingMode.PKCS7;
                aesAlg.Mode = CipherMode.ECB;
                aesAlg.Key = keyBytes;
                aesAlg.IV = keyBytes;
                ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
                using (MemoryStream msEncrypt = new MemoryStream(inputBytes))
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srEncrypt = new StreamReader(csEncrypt))
                        {
                            return srEncrypt.ReadToEnd();
                        }
                    }
                }
            }
        }
    }
}